//index.js
//获取应用实例
const app = getApp()
var md5 = require('../md5.js');
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
  
  //判断是否缓存展会id,用户id
  



  //  var sign = '8b790e0fb4484a17bd726dc4b060a0c8clientiospara2yestimestamp14358259928b790e0fb4484a17bd726dc4b060a0c8'
   // var b64 = md5.hexMD5(sign)
   // console.log('b64='+b64)
    //测试
        /**
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = 'http://dev.api.picoa1.com/Attendee/user/sendcode'
   // var url = 'http://dev.api.picoa1.com/Attendee/Version/Check'
    var t = {}
    t["v"] = '1.0.0'
    t["client"] = 'ios'
    //t["para2"] = 'yes'
    t["timestamp"] = timestamp
    t["to"] = '17710231191'
    t["sendtype"] = '1'
   t["lan"] = 'cn'
  //  var sign = '8b790e0fb4484a17bd726dc4b060a0c8clientiossendtype1timestamp' + timestamp +'to177102311918b790e0fb4484a17bd726dc4b060a0c8'
  //  var sign1 = '8b790e0fb4484a17bd726dc4b060a0c8clientiostimestamp' + timestamp + '8b790e0fb4484a17bd726dc4b060a0c8'
  //  console.log('sign=' + sign)
    var sign1 = '8b790e0fb4484a17bd726dc4b060a0c8clientiossendtype1timestamp' + timestamp + 'to177102311918b790e0fb4484a17bd726dc4b060a0c8'
    console.log('sign1=' + sign1)
  //  var sl = '8b790e0fb4484a17bd726dc4b060a0c8clientudppara2yestimestamp14358259928b790e0fb4484a17bd726dc4b060a0c8'
 //   var slb64 = md5.hexMD5(sl)
    var b64 = md5.hexMD5(sign1)
  // b64 = b64.toUpperCase()
 //   slb64 = slb64.toUpperCase()
    console.log('b64=' + b64)
 //   console.log('slb64=' + slb64)
    t["sign"] = b64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url:url,
      method: 'post',
      data: t,
      success(res) {
        console.log(res)
      }, fail: res => {
        console.log(res, "error")
      }
    })

    var timestamp1 = Date.parse(new Date());
    timestamp1 = timestamp1 / 1000;
    var url1 = 'http://dev.api.picoa1.com/Attendee/user/login'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    //t["para2"] = 'yes'
    k["timestamp"] = timestamp1
    //   t["to"] = '17710231191'
    //   t["sendtype"] = '1'
    k["lan"] = 'cn'
    k["username"]='17710231191'
    k["verificationcode"]='421574'
    k["logintype"]='1'
   // k["password"]='A123456'
    var sign2 = '8b790e0fb4484a17bd726dc4b060a0c8clientioslogintype1timestamp' + timestamp1 + 'username17710231191verificationcode4215748b790e0fb4484a17bd726dc4b060a0c8'
    var curb64 = md5.hexMD5(sign2)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url1,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
      }, fail: res => {
        console.log(res, "error")
      }
    })

    var timestamp1 = Date.parse(new Date());
    timestamp1 = timestamp1 / 1000;
    var url1 = 'http://dev.api.picoa1.com/attendee/contacts/list'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    //t["para2"] = 'yes'
    k["timestamp"] = timestamp1
    //   t["to"] = '17710231191'
    //   t["sendtype"] = '1'
    k["lan"] = 'cn'
    k["exhibitionid"] = '1'
    k["u"] = '142'
   // k["page"] = '1'
   // k["recommend"] = '-1'
   // k["status"] = '-1'
    k["isrecommend"] = '0'
    //k["relationid"] = '0'
    // k["password"]='A123456'
    var sign2 = '8b790e0fb4484a17bd726dc4b060a0c8clientiosexhibitionid1isrecommend0timestamp' + timestamp1 + '8b790e0fb4484a17bd726dc4b060a0c8'
    console.log(sign2)
    var curb64 = md5.hexMD5(sign2)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url1,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg='+mesg)
        var curdata = mesg.data
        var size = curdata.length;
        if (size!=0){
          var fullname = curdata[0].fullname
          console.log('fullname=' + fullname)
        }
   
      }, fail: res => {
        console.log(res, "error")
      }
      
    })
    */

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
 makePhone: function (e) {
   wx.makePhoneCall({
     phoneNumber: '95559',
     success: function (res) { },
     fail: function (res) { },
     complete: function (res) { },
   })
  }
})
